package com.huluobo.high3videoproject.mvp.video.view;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.huluobo.base.view.BaseFragment;
import com.huluobo.high3videoproject.R;
import com.huluobo.high3videoproject.adapter.VideoCardAdapter;
import com.huluobo.high3videoproject.bean.VideoBeanResult;
import com.huluobo.high3videoproject.mvp.fullscreen.view.VideoActivity;
import com.huluobo.high3videoproject.mvp.video.presenter.VideoContract;
import com.huluobo.high3videoproject.mvp.video.presenter.VideoPresenter;
import com.huluobo.high3videoproject.net.Constant;

import java.util.ArrayList;
import java.util.List;

public class VideoListFragment extends BaseFragment<VideoContract.View, VideoPresenter>
        implements VideoContract.View {

    private RecyclerView recyclerView;
    private VideoCardAdapter adapter;

    private List<VideoBeanResult.VideoBean> videoBeans = new ArrayList<>();

    @Override
    public void onGetVideoListSuccess(VideoBeanResult videoBeanResult) {
        videoBeans.addAll(videoBeanResult.getData());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onGetVideoListFailed(String error) {
        Log.i("TagA", "error:" + error);
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.fragment_video_list;
    }

    @Override
    protected void initData() {
        presenter = new VideoPresenter();
        presenter.getVideoList(1, 10);
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.video_list_recycleView);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

        adapter = new VideoCardAdapter(videoBeans);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                Intent intent = new Intent(getActivity(), VideoActivity.class);
                intent.putExtra(Constant.INTENT_VIDEO_PATH, videoBeans.get(position).getVideopath());
                getActivity().startActivity(intent);
            }
        });
    }
}
